package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.QueryAdminDTO;
import com.woniu.mapper.AdminMapper;
import com.woniu.pojo.Admin;
import com.woniu.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Title
 * @Author 武术有栖
 * @Description:
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin adminIsExist(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    public void saveToken(Admin admin) {
        adminMapper.updateById(admin);
    }

    @Override
    public Admin getByToken(String token) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("token",token);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    @Override
    public PageData select(Integer currentPage, Integer pageSize, QueryAdminDTO adminDTO) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminDTO.getName())){
            wrapper.like("name", adminDTO.getName());
        }
        if(!StringUtils.isEmpty(adminDTO.getCreateTimeStart())){
            wrapper.ge("create_time", adminDTO.getCreateTimeStart());
        }
        if(!StringUtils.isEmpty(adminDTO.getCreateTimeEnd())){
            wrapper.le("create_time", adminDTO.getCreateTimeEnd());
        }
        IPage<Admin> page = new Page<>(currentPage, pageSize);
        IPage<Admin> selectPage = adminMapper.selectPage(page, wrapper);
        PageData pageData = new PageData(selectPage.getTotal(), selectPage.getRecords());
        System.out.println(pageData);
        return pageData;
    }

    @Override
    public Boolean getByName(String name) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin == null;
    }

}
